//
//  SelectPictureView.m
//  chongwu
//
//  Created by 隼目 on 2021/9/17.
//

#import "SelectPictureView.h"

@implementation SelectPictureView

-(void)creatView{
    self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    UIView *topView=[UIView new];
    [self addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-638*_mainHeight);
    }];
    topView.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectRemovew)];
    [topView addGestureRecognizer:tapGesture2];
    
    UIView *botoomView=[UIView new];
    [self addSubview:botoomView];
    [botoomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(topView.mas_bottom);
    }];
    
    UIImageView *bottomImage=[UIImageView new];
    [botoomView addSubview:bottomImage];
    [bottomImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    bottomImage.image=[UIImage imageNamed:@"beijing-1"];
    
    UIButton *topBtn=[UIButton new];
    [botoomView addSubview:topBtn];
    [topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mainWidth*33);
        make.right.mas_equalTo(-45*_mainWidth);
        make.height.mas_equalTo(121*_mainHeight);
        make.top.mas_equalTo(_mainHeight*111.7);
    }];
    topBtn.backgroundColor=rgba(255,238,226,1);
    topBtn.layer.masksToBounds=YES;
    topBtn.layer.cornerRadius=7*_mainHeight;
    [topBtn setTitle:@"拍照" forState:0];
    [topBtn setTitleColor:rgba(47,47,47,1) forState:0];
    topBtn.titleLabel.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(38)];
    [topBtn addTarget:self action:@selector(SelectCameraBtn) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *centerBtn=[UIButton new];
    [botoomView addSubview:centerBtn];
    [centerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mainWidth*33);
        make.right.mas_equalTo(-45*_mainWidth);
        make.height.mas_equalTo(121*_mainHeight);
        make.top.mas_equalTo(topBtn.mas_bottom);
    }];
    [centerBtn setTitle:@"从手机相册选择" forState:0];
    [centerBtn setTitleColor:rgba(47,47,47,1) forState:0];
    centerBtn.titleLabel.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(38)];
    [centerBtn addTarget:self action:@selector(selectAlbumBtn) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *bottomBtn=[UIButton new];
    [botoomView addSubview:bottomBtn];
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_mainWidth*33);
        make.right.mas_equalTo(-45*_mainWidth);
        make.height.mas_equalTo(121*_mainHeight);
        make.bottom.mas_equalTo(-_mainHeight*121);
    }];
    bottomBtn.layer.borderWidth=2*_mainHeight;
    bottomBtn.layer.borderColor=rgba(255,102,0,1).CGColor;
    bottomBtn.layer.masksToBounds=YES;
    bottomBtn.layer.cornerRadius=111.7*_mainHeight/2;
    [bottomBtn setTitle:@"取消" forState:0];
    [bottomBtn setTitleColor:rgba(255,103,0,1) forState:0];
    bottomBtn.titleLabel.font=[UIFont fontWithName:@"PingFang SC" size:autoScaleW(38)];
    bottomBtn.layer.shadowColor = rgba(196,50,0,0.36).CGColor;
    bottomBtn.layer.shadowOffset = CGSizeMake(0,0);
    bottomBtn.layer.shadowOpacity = 0.5;
    bottomBtn.layer.shadowRadius = 0.5;
    [bottomBtn addTarget:self action:@selector(selectRemovew) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)selectRemovew{
    [self removeFromSuperview];
}

-(void)SelectCameraBtn{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    
    UIImagePickerControllerQualityType qualityType = UIImagePickerControllerQualityTypeHigh;
    
    /*判断是否有相机*/
    
    if([UIImagePickerController isSourceTypeAvailable : UIImagePickerControllerSourceTypeCamera]){
        
        UIImagePickerController *picker = [[UIImagePickerController alloc]init];
        
        picker.delegate=self;
        
        /*设置拍照后的图片可被编辑*/
        
        picker.allowsEditing=YES;
        
        /*资源类型为照相机*/
        
        picker.sourceType= sourceType;
        
        /*品质为高*/
        
        picker.videoQuality= qualityType;
        
        [self.controller presentViewController: picker animated:YES completion:^{
            
            
        }];
        
    }else{
        NSLog(@"无相机");
    }
    
}
-(void)selectAlbumBtn
{
    
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    
    /*资源类型为图片库*/
    
    picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
    
    picker.delegate=self;
    
    /*设置选择后的图片可被编辑*/
    
    picker.allowsEditing=YES;
    
    [self.controller presentViewController:picker animated:YES completion:^{
        
        
    }];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        self.SelectPictureBlock(image);
        [self selectRemovew];
       
    }];
    
}
@end
